package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseTimeDialogFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.utils.DateUtil;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;

public class WorkRequestBaseFragment extends BaseFragment implements OnClickListener {

  protected static final int REQUEST_FINISHTIME = 0;
  protected static final int REQUEST_CORPORATOR = 1;
  protected EditText mRequestIntroduction_workRequest; // 申办事宜说明
  protected TextView mCompleteTime_workRequest;// 要求完成时间
  protected TextView mCorporator_WorkRequest;// 申请协作人
  protected TextView mRegister_workRequest;// 登记人
  protected TextView mRegisterTime_workRequest;// 登记时间
  protected TextView mDataStatus_workRequest;// 数据状态
  protected CurrentUser mCurrentUser;
  protected String mRegister;// 登记人
  protected String mCorporator;// 协作人
  protected String mRegistjobNos;// 登记人的jobNumber
  protected String mRequestFnishTime;
  protected BaseVolleyActivity mActivity;
  protected String mRequestIntroduction;// 申办事宜说明

  @Override public void onCreate(Bundle savedInstanceState) {
    mCurrentUser = CurrentUser.newInstance(getActivity());
    mRegister = mCurrentUser.getRealName();
    mActivity = (BaseVolleyActivity) getActivity();
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_add_work_request, container, false);
    mRequestIntroduction_workRequest =
        (EditText) view.findViewById(R.id.et_requestIntroduction_workRequest);
    mCompleteTime_workRequest = (TextView) view.findViewById(R.id.tv_completeTime_workRequest);
    mCorporator_WorkRequest = (TextView) view.findViewById(R.id.tv_corporator_WorkRequest);
    mRegister_workRequest = (TextView) view.findViewById(R.id.tv_register_workRequest);
    mRegisterTime_workRequest = (TextView) view.findViewById(R.id.tv_registerTime_workRequest);
    mDataStatus_workRequest = (TextView) view.findViewById(R.id.tv_dataStatus_workRequest);

    mRegister_workRequest.setText(mRegister);
    mRegisterTime_workRequest.setText(MyDateUtil.dateToString(new Date(), MyDateUtil.TYPE_DATE));
    mDataStatus_workRequest.setText(getString(R.string.draft));
    mCompleteTime_workRequest.setOnClickListener(this);
    mCorporator_WorkRequest.setOnClickListener(this);

    initView();
    return view;
  }

  protected void initView() {

  }

  @Override public void onClick(View v) {
    switch (v.getId()) {
      // 要求完成时间
      case R.id.tv_completeTime_workRequest:
        showDialog(REQUEST_FINISHTIME, null);
        break;
      // 申请协作人
      case R.id.tv_corporator_WorkRequest:
        showDialog(REQUEST_CORPORATOR, new Date());
        break;

      default:
        break;
    }
  }

  protected void showDialog(int requestCode, Object object) {
    DialogFragment dialog = null;

    switch (requestCode) {
      // 要求完成时间
      case REQUEST_FINISHTIME:
        dialog = BaseTimeDialogFragment.newInstance(new Date(), R.string.chooseAcquireTime);
        break;
      // 配合人
      case REQUEST_CORPORATOR:
        //Intent i = new Intent(getActivity(), AppointerListActivity.class);
        //startActivityForResult(i, requestCode);
        Intent i = new Intent(getActivity(), SearchPersonMultipleActicity.class);
        i.putExtra(SearchPersonMultipleFragment.EXTRA_FROM,
            SearchPersonMultipleFragment.VALUE_COMMON);
        i.putExtra(SearchPersonMultipleFragment.EXTRA_JOB,
            TextUtils.isEmpty(mRegistjobNos) ? "" : mRegistjobNos);
        startActivityForResult(i, requestCode);
        return;

      default:
        break;
    }
    FragmentManager fm = getActivity().getSupportFragmentManager();
    dialog.setTargetFragment(WorkRequestBaseFragment.this, requestCode);
    dialog.show(fm, "");
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    // 协作人
    if (requestCode == REQUEST_CORPORATOR && data != null) {
      mCorporator = data.getStringExtra(SearchPersonMultipleFragment.EXTRA_NAME);
      mRegistjobNos = data.getStringExtra(SearchPersonMultipleFragment.EXTRA_JOB);
      mCorporator_WorkRequest.setText(mCorporator);
    }
    // 要求完成时间
    if (requestCode == REQUEST_FINISHTIME && data != null) {
      Date acquireDate = (Date) data.getSerializableExtra(BaseTimeDialogFragment.EXTRA_DATE);
      mRequestFnishTime = DateUtil.dateToString(acquireDate, DateUtil.TYPE_ALL);
      mCompleteTime_workRequest.setText(mRequestFnishTime);
    }

    super.onActivityResult(requestCode, resultCode, data);
  }
}
